﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon.Prompt;

namespace Baosight.ZNH.ZMCE.UI.PopWindows
{
    public partial class EquipmentCardCopy : EFViewCmes
    {
        public EquipmentCardCopy()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 构造提交到后台的EiInfo
        /// </summary>
        /// <param name="BlockId"></param>
        /// <returns></returns>
        public EIInfo BuildSubmitEiInfo1(string BlockId)
        {
            EIInfo subInfo = new EIInfo();
            subInfo.AddBlock(EIConstants.ResultBlock).SetColumns(GetDataFieldColumns(BlockId));
            subInfo.Attributes.Add(EIConstants.ServiceNameKey, eServiceName);
            subInfo.Attributes.Add(EIConstants.MethodNameKey, "copy");

            //如果结束编号长度为空或者0，则只复制一条数据
            if (this.WildcardLength.Value == null || "0".Equals(this.WildcardLength.Value) || "".Equals(this.WildcardLength.Value))
            {
                EIRow row = GetDataFieldValues(BlockId);
                subInfo.GetBlock(EIConstants.ResultBlock).AddRow(row);
                return subInfo;
            }

            for (int i = int.Parse(this.StartNum.Value.ToString()); i <= int.Parse(this.EndNum.Value.ToString()); i++)
            {
                EIRow row = GetDataFieldValues(BlockId);
                string code = row["fdSuffix"].ToString();
                row["fdSuffix"] = code + BaseTools.GetNumString(i, int.Parse(this.WildcardLength.Value.ToString()));
                subInfo.GetBlock(EIConstants.ResultBlock).AddRow(row);
            }
            return subInfo;
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;

            if (!this.ValidateField())
            {
                this.ValidateMessagePopup();//弹出提示信息
                this.TempButtonOpen(true);//确定按钮生效
            }
            else
            {
                if (!(this.WildcardLength.Value == null || "0".Equals(this.WildcardLength.Value) || "".Equals(this.WildcardLength.Value)))
                {
                    //判断结束编号中前一个输入框应该小于后面的
                    int start = 0, end = 0;
                    int.TryParse(this.StartNum.Value.ToString(), out start);
                    int.TryParse(this.EndNum.Value.ToString(), out end);
                    if (end < start)
                    {
                        this.ValidateMessagePopup("输入项“从...到...”中，后面的数字应该大于前面的数字！");
                        this.TempButtonOpen(true);//确定按钮生效
                        return;
                    }
                }
                base.Submit(this.BuildSubmitEiInfo1("result"), DoCommitCompleted);
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 设备分类改变后修改一些字段的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdEquipmentClassId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null || "".Equals(e.NewValue))
            {
                return;
            }
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker.ItemObject != null)
            {
                this.fdToencode.Value = picker.ItemObject.GetType().GetProperty("fdToencode").GetValue(picker.ItemObject, null).ToString();
            }
        }

        /// <summary>
        /// 使用状态改变，部门和工程是否必填
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdUsingType_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            if (!("在库".Equals(e.NewValue.ToString()) || "报废".Equals(e.NewValue.ToString())))
            {
                this.fdDepartmentId.Label = "*使用部门";
                this.fdDepartmentId.eRegex = "^.+$";
                this.fdDepartmentId.eValidateErrorPrompt = "不能为空";
                return;
            }
            this.fdDepartmentId.Label = "使用部门";
            this.fdDepartmentId.eRegex = "";
            this.fdDepartmentId.eValidateErrorPrompt = "";
        }

        /// <summary>
        /// 部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdDepartmentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                this.fdDepartment.Value = box.SelectedItem.GetType().GetProperty("orgName").GetValue(box.SelectedItem, null).ToString();
            }
        }

        /// <summary>
        /// 工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdEngineeringId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                this.fdEngineering.Value = box.SelectedItem.GetType().GetProperty("fdName").GetValue(box.SelectedItem, null).ToString();
            }
        }


        /// <summary>
        /// 编号结尾长度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WildcardLength_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            int num = 0;
            if (this.WildcardLength.Value != null)
            {
                int.TryParse(this.WildcardLength.Value.ToString(), out num);
            }

            this.fdCode.eRegex = "^[a-zA-Z0-9]{0," + (7 - num) + "}$";
            this.fdCode.eValidateErrorPrompt = "输入0~" + (7 - num) + "个数字、字母";

            if (num > 0)
            {
                this.StartNum.eRegex = @"^\d{1," + num + "}$";
                this.StartNum.eValidateErrorPrompt = "输入长度不超过" + num + "位的整数";
                this.EndNum.eRegex = @"^\d{1," + num + "}$";
                this.EndNum.eValidateErrorPrompt = "输入长度不超过" + num + "位的整数";
            }
            else
            {
                this.fdCode.eLabel = "起始部分编号";
                this.fdCode.eRegex = @"^[a-zA-Z0-9]{2,7}$";
                this.fdCode.eValidateErrorPrompt = "输入2~7个数字、字母";

                this.StartNum.eRegex = "";
                this.StartNum.eValidateErrorPrompt = "";
                this.EndNum.eRegex = "";
                this.EndNum.eValidateErrorPrompt = "";
            }
        }

        /// <summary>
        /// 验证失败，消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }

    }
}
